package pers.qianyu.month_202012.date_20201210;

import org.junit.*;

import java.util.*;

/**
 * 78. 子集
 * https://leetcode-cn.com/problems/subsets/
 *
 * @author mizzle rain
 * @date 2020-12-10 17:07
 */
public class Subsets {
    /**
     * 数学归纳法 + 递归实现
     */
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return res;
        }
        res.add(new ArrayList<>());
        recur(res, nums, 0);
        return res;
    }

    private void recur(List<List<Integer>> res, int[] nums, int i) {
        if (i < nums.length) {
            int num = nums[i];
            int size = res.size();
            for (int k = 0; k < size; k++) {
                List<Integer> list = res.get(k);
                List<Integer> newList = new ArrayList<>(list);
                newList.add(num);
                res.add(newList);
            }
            recur(res, nums, i + 1);
        }
    }

    @Test
    public void test1() {
        int[] arr = {1, 2, 3};
        List<List<Integer>> result = new Subsets().subsets(arr);
        result.forEach(System.out::println);
    }
}
